'use strict';

/**
 * @ngdoc function
 * @name elmApp.controller:HomeCtrl
 * @description
 * # HomeCtrl
 * Controller of the elmApp
 */
angular.module('fscApp')
    .controller('LinkmanCtrl', function ($scope, $window, $location, resourcePool,
                                         global, utils, constants, sync, socket) {
        $scope.classes = global.cache.classes;
        $scope.teachers = global.cache.teachers;
        var w = angular.element($window);
        $scope.winHeight = w.height();
        $scope.isGoing = false;
        if(!global.cache.teachers){
            sync.initUserList();
            $scope.classes = global.cache.classes
            $scope.teachers = global.cache.teachers
            $scope.deans = global.cache.deans
            $scope.groups = global.pageStatus.linkman.groups;
        }
        $scope.selectLinkman = function (student) {
            $scope.selectUser = student;
            global.pageStatus.linkman.selectUser = student;
        };
        var ParentsStudents = resourcePool.parentsStudents;
        $scope.selectParent = function (parent) {
            ParentsStudents.query({parentId: parent.id}, function (parentStudent) {
                $scope.selectUser.parentStudents = parentStudent;
            });
            $scope.selectUser = parent;
            global.pageStatus.linkman.selectUser = parent;
        };
        $scope.headerClick = function (code, index) {
            if (code == "class") {
                var class_ = $scope.classes[index];
                class_.open = (!class_.open);
            } else if (code == "class_parent") {
                var class_ = $scope.classes[index];
                class_.open_parents = (!class_.open_parents);
            } else {
                var group = $scope.groups[code];
                group.open = (!group.open);
            }
        };
        $scope.$watch('keywords',function (val) {
            if(val){
                $scope.classes.forEach(function(class_){
                    class_.open =true
                    class_.open_parents = true;
                })
                $scope.groups["teacher"].open = true
                $scope.groups["dean"]&&($scope.groups["dean"].open = true)
            }else{
                $scope.classes.forEach(function(class_){
                    class_.open =false
                    class_.open_parents = false;
                })
                $scope.groups["teacher"].open = false
                $scope.groups["dean"]&&($scope.groups["dean"].open = false)
            }
        })
        var Sessions = resourcePool.sessions;
        $scope.goSession = function(userId){
            var sessions = global.cache.sessions;
            var selectUserId=userId?userId:$scope.selectUser.id;
            if(sessions){
                var sessionExists = false;
                for (var i = 0; i < sessions.length; i++) {
                    var session = sessions[i];
                    if(session.type==constants.session.user){
                        if(session.msId== selectUserId){
                            global.pageStatus.session.selectSession = session;
                            global.pageStatus.session.toTop = true;
                            global.pageStatus.session.selectSession.show = true;
                            $location.path('/session');
                            sessionExists = true;
                        }
                    }
                }
                if(!sessionExists){
                    $scope.isGoing = true;
                        Sessions.create({linkmanId: selectUserId}, {}, function (data) {
                            socket.emit("notify", {userIdArray: [selectUserId], reqCode: "NOTIFY_PULL_FSC_LINKMAN_ACCEPT"})
                            sync.syncSessions(function(sessions){
                            for (var i = 0; i < sessions.length; i++) {
                                var session = sessions[i];
                                if(session.type==constants.session.user){
                                    if(session.msId== selectUserId){
                                        global.pageStatus.session.selectSession = session;
                                        global.pageStatus.session.toTop = true;
                                        global.pageStatus.session.selectSession.show = true;
                                        $location.path('/session');
                                        sessionExists = true;
                                    }
                                }
                            }
                            $scope.isGoing = false;
                        });
                    });
                }
            }
        };

        $scope.$on('$viewContentLoaded', function () {
            $scope.selectUser = global.pageStatus.linkman.selectUser;
        });

    });
